package own.stu.inter.loadbalance.ping;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import own.stu.inter.loadbalance.server.ServerInstance;

import java.util.ArrayList;
import java.util.List;

public class SerialPingStrategy implements IPingStrategy {

    private static Logger logger = LoggerFactory.getLogger(SerialPingStrategy.class);

    @Override
    public List<Boolean> pingServers(IPing ping, List<ServerInstance> instances) {

        List<Boolean> results = new ArrayList<>(instances.size());

        logger.debug("LoadBalancer pingServers executing [{}] instances", instances);

        for (ServerInstance instance : instances) {
            try {
                if (ping != null) {
                    results.add(ping.isAlive(instance));
                }
            } catch (Exception e) {
                logger.error("Exception while pinging Server: '{}'", instances, e);
            }
        }
        return results;
    }
}